package com.zbcz.crm.adapter

import InnerListAdapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.zbcz.crm.CrmApplication
import com.zbcz.crm.R
import com.zbcz.crm.bean.InnerListBean
import com.zbcz.crm.bean.VisitData
import com.zbcz.crm.databinding.ItemVisitDataBinding
import com.zbcz.crm.util.onSingleClick
import com.zbcz.crm.util.setRoundedCorners

class VisitDataListAdapter(val mDatas: List<VisitData>) :
    RecyclerView.Adapter<VisitDataListAdapter.VisitDataHolder>() {

    private val expandedStates = mutableMapOf<Int, Boolean>()

    inner class VisitDataHolder(private val binding: ItemVisitDataBinding) :
        ViewHolder(binding.root) {

        fun bind() {
            val mData = mDatas[adapterPosition]
            if (mData.visitOrderStatus == "1") {
                binding.topImage.setImageResource(R.drawable.agree_top_bg)
                binding.tvStatus.text = "已同意"
                binding.tvFold.setTextColor(
                    ContextCompat.getColor(
                        CrmApplication.mApplication,
                        R.color.blue_text_color
                    )
                )
                binding.ivFold.setImageResource(R.drawable.arrow_blue)
            } else {
                binding.topImage.setImageResource(R.drawable.disagree_top_b)
                binding.ivFold.setImageResource(R.drawable.arrow_red)
                binding.tvStatus.text = "已驳回"
                binding.tvFold.setTextColor(
                    ContextCompat.getColor(
                        CrmApplication.mApplication,
                        R.color.red_text_color
                    )
                )
            }
            binding.rcList.adapter = InnerListAdapter().apply {
                submitList(mData.toInnerList())
            }
            binding.rcList.setRoundedCorners(45f)
            binding.rcList.setRoundedCorners(45f)
            binding.tvFold.onSingleClick {
                toggleItemExpansion(adapterPosition)
            }
            binding.tvFold.text = CrmApplication.mApplication.getString(
                if (expandedStates[position] ?: true) R.string.fold else R.string.expand
            )
        }

        fun setExpanded(expanded: Boolean) {
//            val layoutParams = binding.rcList.layoutParams as ConstraintLayout.LayoutParams
//            layoutParams.height = if (expanded) ConstraintLayout.LayoutParams.WRAP_CONTENT else 0
//            binding.rcList.layoutParams = layoutParams
//            binding.rcList.visibility = if (expanded) View.VISIBLE else View.GONE
            val mData = mDatas[adapterPosition]
            binding.rcList.adapter?.let { mAdapter ->
                if (mAdapter is InnerListAdapter) {
                    mAdapter.submitList(if (expanded) mData.toInnerList() else mData.toBriefInnerList())
                }
            }
        }


        private fun VisitData.toBriefInnerList(): List<InnerListBean> {
            val innerList = mutableListOf<InnerListBean>()
            innerList.add(InnerListBean("拜访人员", visitingPersonName))
            innerList.add(InnerListBean("访问目的", visitPurpose))
            return innerList
        }

        private fun VisitData.toInnerList(): List<InnerListBean> {
            val innerList = mutableListOf<InnerListBean>()
            innerList.add(InnerListBean("访问人员", visitorName))
            innerList.add(InnerListBean("身份证号", visitorIdNo))
            innerList.add(InnerListBean("访客工作单位", visitorEmployer))
            innerList.add(InnerListBean("是否携带电子产品", carryingElectronic))
            innerList.add(InnerListBean("访问目的", visitPurpose))
            innerList.add(InnerListBean("访问区域", visitArea))
            innerList.add(InnerListBean("拜访人员", visitingPersonName))
            if (this.followPersonVos?.isNotEmpty() == true) {
                for (i in this.followPersonVos!!.indices) {
                    val follower = followPersonVos!![i]
                    innerList.add(
                        InnerListBean(
                            "随访人员",
                            content = follower.followName,
                            followName = follower.followName,
                            followEmployer = follower.followEmployer,
                            followMobileNo = follower.followMobileNo,
                            isFirstFollower = i == 0
                        )
                    )
                }
            }
//            innerList.add(InnerListBean("访问日期", visitDate, null))
//            innerList.add(InnerListBean("访问单状态", visitOrderStatus, null))
//            innerList.add(InnerListBean("拜访人员手机号码", visitingPersonMobileNo, null))
//            innerList.add(InnerListBean("手机号", visitorMobileNo, null))
            return innerList
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VisitDataHolder {
        val inflater = LayoutInflater.from(parent.context)
        val binding = ItemVisitDataBinding.inflate(inflater, parent, false)
        return VisitDataHolder(binding)
    }

    override fun getItemCount(): Int {
        return mDatas.size
    }

    override fun onBindViewHolder(holder: VisitDataHolder, position: Int) {
        holder.bind()
        // 设置展开或折叠的状态
        holder.setExpanded(expandedStates[position] ?: true)
    }

    private fun toggleItemExpansion(position: Int) {
        expandedStates[position] = !(expandedStates[position] ?: true)
        notifyItemChanged(position)
    }
}